package de.szut.internalorganizer;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Class for loading sudokufields, was originally a widget that got modified a
 * bit
 * 
 * @author dqi12huisinga
 * 
 */
public class InternalLoader extends InternalOrganizer {
	private boolean lockFileAfterReading;

	public InternalLoader() {
		setLockFileAfterReading(false);
	}

	public boolean isLockFileAfterReading() {
		return lockFileAfterReading;
	}

	public void setLockFileAfterReading(boolean lockFileAfterReading) {
		this.lockFileAfterReading = lockFileAfterReading;
	}

	/**
	 * loads the data
	 * 
	 * @param fileName
	 * @param identifier
	 * @return
	 * @throws IOException
	 */
	public String[][][] loadData(String fileName, String identifier)
			throws IOException {
		// ArrayLists for all 3 fields
		ArrayList<String[]> data = new ArrayList<String[]>();
		ArrayList<String[]> data2 = new ArrayList<String[]>();
		ArrayList<String[]> data3 = new ArrayList<String[]>();
		File loadFile = getFile();
		// Saves the lock-state the file is in
		boolean isReadable = false;
		if (loadFile.canRead()) {
			isReadable = true;
		}
		boolean isWritable = false;
		if (loadFile.canWrite()) {
			isWritable = true;
		}
		// Unlocks the file if necessary
		loadFile.setReadable(true);
		loadFile.setWritable(true);

		BufferedReader bufferedReader = new BufferedReader(new FileReader(
				loadFile));
		// Reads the identifier
		String currentLine = bufferedReader.readLine();
		// Only continues reading if the file has the respective identifier
		if (currentLine.equals(identifier)) {
			// Reads the file and saves the required information
			currentLine = bufferedReader.readLine();
			while (!currentLine.equals("")) {
				data.add(currentLine.split(getDataSeparator()));
				currentLine = bufferedReader.readLine();
			}
			// Reads the file and saves the required information
			currentLine = bufferedReader.readLine();
			while (!currentLine.equals("")) {
				data2.add(currentLine.split(getDataSeparator()));
				currentLine = bufferedReader.readLine();
			}
			// Reads the file and saves the required information
			currentLine = bufferedReader.readLine();
			while (currentLine != null) {
				data3.add(currentLine.split(getDataSeparator()));
				currentLine = bufferedReader.readLine();
			}
		}
		bufferedReader.close();

		// Locks the file if necessary
		if (!isReadable) {
			loadFile.setReadable(false);
		}
		if (!isWritable) {
			loadFile.setWritable(false);
		}

		// Converts the ArrayLists to arrays
		String[][] endData1 = new String[data.size()][data.get(0).length];
		int xIndex = 0;
		for (String[] currentArray : data) {
			int yIndex = 0;
			for (String currentElem : currentArray) {
				endData1[xIndex][yIndex] = currentElem;
				yIndex++;
			}

			xIndex++;
		}
		String[][] endData2 = new String[data2.size()][data2.get(0).length];
		xIndex = 0;
		for (String[] currentArray : data2) {
			int yIndex = 0;
			for (String currentElem : currentArray) {
				endData2[xIndex][yIndex] = currentElem;
				yIndex++;
			}

			xIndex++;
		}
		String[][] endData3 = new String[data3.size()][data3.get(0).length];
		xIndex = 0;
		for (String[] currentArray : data3) {
			int yIndex = 0;
			for (String currentElem : currentArray) {
				endData3[xIndex][yIndex] = currentElem;
				yIndex++;
			}

			xIndex++;
		}

		String[][][] endData = { endData1, endData2, endData3 };
		return endData;
	}
}
